package com.olddriver.crm.service.impl;

import com.olddriver.crm.domain.AccidentBillItem;
import com.olddriver.crm.domain.Claims;
import com.olddriver.crm.domain.Employee;
import com.olddriver.crm.domain.PaidList;
import com.olddriver.crm.mapper.AccidentBillItemMapper;
import com.olddriver.crm.mapper.ClaimsMapper;
import com.olddriver.crm.mapper.PaidListMapper;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.ClaimsQueryObject;
import com.olddriver.crm.service.IClaimsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClaimsServiceImpl implements IClaimsService {

    @Autowired
    private ClaimsMapper claimsMapper;
    @Autowired
    private AccidentBillItemMapper accidentBillItemMapper;

    @Autowired
    private PaidListMapper paidListMapper;


    @Override
    public void deleteByPrimaryKey(Long id) {
        claimsMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void insert(Claims record) {
        claimsMapper.insert(record);

    }

    @Override
    public Claims selectByPrimaryKey(Long id) {
        Claims claim = claimsMapper.selectByPrimaryKey(id);
        return claim;
    }

    @Override
    public List<Claims> selectAll() {
        List<Claims> claims = claimsMapper.selectAll();
        return claims;
    }

    @Override
    public void updateByPrimaryKey(Claims record) {
        claimsMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult query(ClaimsQueryObject qo) {
        int total = claimsMapper.queryTotal(qo);
        if (total == 0) {
            return new PageResult(total, Collections.emptyList());
        }
        return new PageResult(total, claimsMapper.queryRows(qo));
    }

    @Override
    public void receiptsVerify(Long id, String insuranceNum) {


        PaidList paidList = paidListMapper.selectReceiptsByInsuranceNum(insuranceNum);
        Date accidentTime = claimsMapper.selectByPrimaryKey(id).getAccidentTime();

        if (paidList != null) {
            Date beginDate = paidList.getBeginDate();
            Date endDate = paidList.getEndDate();

            int date1 = accidentTime.compareTo(beginDate);
            int date2 = accidentTime.compareTo(endDate);
            //返回 0 表示时间日期相同
            //返回 1 表示日期 1 在 日期 2 之后
            //返回 -1表示日期 1 在日期 2 之前
            if (date2 > 0) {
                claimsMapper.changeBillState(id, 2);//保单过期
            }
            if (date1 == 0 || date2 == 0 || (date1 > 0 && date2 < 0)) {
                claimsMapper.changeBillState(id, 3);//有效
            }
        } else {
            claimsMapper.changeBillState(id, 0);//没有该保单

        }
    }

    //受理,新增一条事故受理单
    @Override
    public void accept(Long id) {
        Claims claim = claimsMapper.selectByPrimaryKey(id);
        AccidentBillItem accident = new AccidentBillItem();
        accident.setClaim(claim);
        accident.setAddress(claim.getAccidentAddress());
        accident.setTel(claim.getTelNumber());
        accident.setPlateNum(claim.getCarNumber());
        accident.setPolicySn(claim.getInsuranceNum());
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        accident.setHandler(employee);
        claimsMapper.deleteByPrimaryKey(id);
        accidentBillItemMapper.insert(accident);
    }
}



